/*! 
 * jquery.populate - v 1.0.0 - http://jquery-ux.com/
 * Copyright (c) 2010 Michael Helgeson, Three Dub Media
 * Open Source MIT License - http://jquery-ux.com/license 
 */
// Created: 2010-02-26
// Updated: 2010-03-01
// REQUIRES: jquery

;(function( $ ){

// jquery instance method
$.fn.populate = function( json, callback ){
	if ( json ){
		// normalize the callback arg
		if ( !$.isFunction( callback ) ) 
			callback = false;
		// remember the jquery collection
		var $this = this, $field;
		// iterate json values
		$.each( json, function( name, value ){
			// look up the element by ID
			$field = $this.find('#'+ name );
			// or else by NAME
			if ( !$field.length ) 
				$field = $this.find('[name='+ name +']');
			// make sure to set an array with multi element	groups
			if ( $field.length > 1 && !$.isArray( value ) )
				value = [ value ];
			// set the value, trigger normal callbacks
			$field.val( value );
			// execute optional callback
			if ( callback ) 
				callback.call( $field, name, value );
				/*** 
				// example callback function
				function( name, value ){ 
					this.trigger( this.is("select") ? "change" : "blur" ); 
				}
				***/
		});
	}
	// preserve jquery call chain
	return this;
};

})( jQuery );